package com.shell.customer.protocol.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.entity.auth.AuthOrgan;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.customer.accreditation.keyword.EsignAuthKeyword;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubDictionaryFeign;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.po.PubProtocol;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PubProtocolController extends BaseController {
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private PubDictionaryFeign pubDictionaryFeign;
	@Autowired
	private CusRecService cusRecService;

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public PubProtocol getAuthProtocol() {
		return convertProtocolContent(this.pubProtocolFeign.getAuthProtocolDetail());
	}

	private PubProtocol convertProtocolContent(PubProtocol pubProtocol) {
		if (pubProtocol != null) {
			String protoType = pubProtocol.getProtoType();
			String content = convertKnownProperties(protoType, pubProtocol.getContent());

			List<PubDictionary> dicts = this.pubDictionaryFeign.selectChildDictByParentCode(protoType);
			for (PubDictionary dct : dicts) {
				String replaceText = dct.getExtension1();
				content = content.replace(replaceText, "");
			}
			pubProtocol.setContent(content);
		}
		return pubProtocol;
	}

	private String convertKnownProperties(String protoType, String content) {
		if ("authen_proto".equals(protoType)) {
			return convertAuthProto(content);
		}
		return content;
	}

	private String convertAuthProto(String content) {
		AuthOrgan authOrgan = CommonDataUtil.getCurEnt();

		CusRec platform = this.cusRecService.getPlatform(null);
		if (platform == null) {
			throw new UnCheckMsgException("����������������");
		}
		EsignAuthKeyword keyword = new EsignAuthKeyword();
		keyword.setEntName(authOrgan.getOrganName());

		keyword.setPlatformNm(platform.getCusName());
		keyword.setPlatformAddress(platform.getAddress());
		keyword.setPlatformLegalNm(platform.getLrName());
		keyword.setPlatformContactPhone(platform.getContactorPhone());
		return ContTemplateHelper.replaceContent(content, keyword);
	}
}
